package com.yljk.servicemanager.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;

import com.yilijk.base.basedialog.BaseDialog;
import com.yilijk.base.basedialog.DialogChain;
import com.yilijk.base.network.RequestParams;
import com.yilijk.base.network.https.HttpCallBack;
import com.yilijk.base.network.https.HttpUtils;
import com.yilijk.base.utils.ALog;
import com.yilijk.base.utils.ActivityManagerUtils;
import com.yilijk.base.utils.DevicesUtils;
import com.yljk.servicemanager.R;
import com.yljk.servicemanager.constants.RouterApi;
import com.yljk.servicemanager.constants.UrlApi;
import com.yljk.servicemanager.interfaceapi.MineBackApi;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2022/01/06
 */
public class IntimacyDialog extends BaseDialog implements View.OnClickListener {


    private String[] urls = new String[]{
            "yszcxy", "flsmjyszc", "ysdjgbalwxy"
    };

    private String[] titles = new String[]{
            "《慧医天下医生注册协议》", "《法律声明及隐私权政策》", "《医师多机构备案劳务协议》"
    };


    public IntimacyDialog(@NonNull Context context) {
        super(context);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout_intimacy);
        findViewById(R.id.intimacy_agree).setOnClickListener(this);
        findViewById(R.id.intimacy_disagree).setOnClickListener(this);
        getRegistText();
        this.setCancelable(false);
        this.setCanceledOnTouchOutside(false);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (chain()!=null){
                    chain().process();
                    ALog.i(getContext(),"医生注册隐私协议弹窗消失");
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.intimacy_agree) {
            agreeButton();
        } else if (id == R.id.intimacy_disagree) {
            //不同意推出登录
            dismiss();
            MineBackApi mineBackApi = (MineBackApi) ARouter.getInstance().build(RouterApi.MINEBACKIMPL).navigation();
            mineBackApi.onCancelLogout(ActivityManagerUtils.getLastestActivity(), getContext());
        }
    }

    @Override
    public void intercept(@NonNull DialogChain chain) {
        super.intercept(chain);

        if (true){
            this.show();
        }else{
            if (chain!=null){
                chain.process();
            }
        }
    }

    @Override
    public void show() {
        super.show();
    }

    /**
     * 同意保存协议
     */

    void agreeButton() {
        RequestParams params = new RequestParams();
        params.put("documentNoList", "[\"yszcxy\",\"flsmjyszc\",\"ysdjgbalwxy\"]");

        HttpUtils.get(UrlApi.preserveAgreement, params, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String result, int code) {
                if (code == 200) {
                    try {
                        JSONObject jsonObject1 = new JSONObject(result);
                        boolean data = jsonObject1.getBoolean("data");
                        if (data) {
                            dismiss();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(String failureMsg, int statusCode) {

            }
        });
    }

    /**
     * 获取注册协议文案
     */
    void getRegistText() {
        final SpannableStringBuilder style = new SpannableStringBuilder();
        final String content = "感谢您对慧医天下互联网医疗平台的信任！为了更好地保护您的个人信息安全和合法权益，请您务必仔细阅读并充分理解我们最新的《慧医天下医生注册协议》、《法律声明及隐私权政策》和《医师多机构备案劳务协议》。我们将严格按照您同意的各项条款使用和保护您的个人信息，并为您提供相关服务。\n" +
                "详细信息请阅读完整版《慧医天下医生注册协议》、《法律声明及隐私权政策》和《医师多机构备案劳务协议》。";
        //设置文字
        style.append(content);

        int a_l = content.indexOf("《慧医天下医生注册协议》");
        int a_L = a_l + "《慧医天下医生注册协议》".length();

        int a_l2 = content.lastIndexOf("《慧医天下医生注册协议》");
        int a_L2 = a_l2 + "《慧医天下医生注册协议》".length();


        int b_l = content.indexOf("《法律声明及隐私权政策》");
        int b_L = b_l + "《法律声明及隐私权政策》".length();

        int b_l2 = content.lastIndexOf("《法律声明及隐私权政策》");
        int b_L2 = b_l2 + "《法律声明及隐私权政策》".length();

        int c_l = content.indexOf("《医师多机构备案劳务协议》");
        int c_L = c_l + "《医师多机构备案劳务协议》".length();

        int c_l2 = content.lastIndexOf("《医师多机构备案劳务协议》");
        int c_L2 = c_l2 + "《医师多机构备案劳务协议》".length();

        style.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                ALog.i(getContext(),"《慧医天下医生注册协议》点击");
                toWebAgreement(1);
            }
        }, a_l, a_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                toWebAgreement(1);
            }
        }, a_l2, a_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        style.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                toWebAgreement(2);
            }
        }, b_l, b_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                toWebAgreement(2);
            }
        }, b_l2, b_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        style.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                toWebAgreement(3);
            }
        }, c_l2, c_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                toWebAgreement(3);
            }
        }, c_l, c_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        style.setSpan(new StyleSpan(Typeface.BOLD), a_l, a_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new StyleSpan(Typeface.BOLD), a_l2, a_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new StyleSpan(Typeface.BOLD), b_l, b_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new StyleSpan(Typeface.BOLD), b_l2, b_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new StyleSpan(Typeface.BOLD), c_l, c_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new StyleSpan(Typeface.BOLD), c_l2, c_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置部分文字颜色
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#1A1A1A")), a_l, a_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#1A1A1A")), a_l2, a_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        style.setSpan(new ForegroundColorSpan(Color.parseColor("#1A1A1A")), b_l, b_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#1A1A1A")), b_l2, b_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        style.setSpan(new ForegroundColorSpan(Color.parseColor("#1A1A1A")), c_l, c_L, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        style.setSpan(new ForegroundColorSpan(Color.parseColor("#1A1A1A")), c_l2, c_L2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        ((TextView)findViewById(R.id.intimacy_tv)).setText(style);

    }

    void toWebAgreement(int i) {
        String url = (DevicesUtils.isEnvironment() ? "https://www.d1.yilijk.com" : "https://www.yilijk.com") + "/document/index.html?id=" + urls[i - 1];
        ALog.i("toWebAgreement", "toWebAgreement: " + url);
        ARouter.getInstance().build("/WebActivity/WebActivity")
                .withString("title", titles[i - 1])
                .withString("url", url)
                .withBoolean("ishtml", false)
                .navigation();
    }
}
